package br.com.lh.urabuilder.helper
{
	import br.com.lh.urabuilder.business.ViewManager;
	import br.com.lh.urabuilder.model.UraModelLocator;
	import br.com.lh.urabuilder.view.ArrowDynamicDrawer;
	import br.com.lh.urabuilder.view.arrow.ArrowContainer;
	import br.com.lh.urabuilder.view.arrow.ArrowWrapper;
	
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.containers.Canvas;
	import mx.core.DragSource;
	import mx.events.DragEvent;
	import mx.managers.DragManager;
	
	public class ArrowDragHelper
	{
		private var parentComponent:Canvas;
		private var viewManager:ViewManager;
		private var mouseEventHelper:MouseEventHelper;
		private var arrowDynamicDrawer:ArrowDynamicDrawer;
		public var arrowWrapper:ArrowWrapper;
		
		public function ArrowDragHelper(parentComponent:Canvas,viewManager:ViewManager,mouseEventHelper:MouseEventHelper){
			this.parentComponent = parentComponent;
			this.viewManager = viewManager;
			this.mouseEventHelper = mouseEventHelper;
			this.arrowDynamicDrawer = new ArrowDynamicDrawer(parentComponent);
		}
		
		public function initDrag(event:MouseEvent,created:Boolean):void{
		 	if (!mouseEventHelper.isMouseMovingArrow && created){
		 		return;
		 	}
            var dragInitiator:ArrowContainer=ArrowContainer(event.currentTarget);
            var ds:DragSource = new DragSource();
            var isEndRotation:Boolean =event.localX > dragInitiator.width * ArrowContainer.END_OF_ARROW_PERCENT;
            var isBeginRotation:Boolean =event.localX < dragInitiator.width*ArrowContainer.START_OF_ARROW_PERCENT;
            ds.addData(isEndRotation,"rotateEnd");
            ds.addData(isBeginRotation,"rotateBegin");
            ds.addData(created, 'created');
//       		var point:Point = new Point(event.localX,event.localY);
//            if (isEndRotation){
//        		arrowDynamicDrawer.rotateArrow(dragInitiator.arrowWrapper.contentToGlobal(dragInitiator.arrowWrapper.startPoint),point);
//        	}else if(isBeginRotation){
//        		arrowDynamicDrawer.rotateArrow(point,dragInitiator.arrowWrapper.contentToGlobal(dragInitiator.arrowWrapper.endPoint));
//        	}
			trace ('localX ' + event.localX);
			trace ('localY ' + event.localY);
			trace ('width ' +dragInitiator.width);
            buildArrowWrapper(new Point(-event.localX,-event.localY -10),new Point(dragInitiator.width -event.localX ,-event.localY -10),dragInitiator.rotation);
            DragManager.doDrag(dragInitiator, ds, event,arrowWrapper);
        }
        
        private function buildArrowWrapper(startPoint:Point,endPoint:Point,angle:Number):void{
        	arrowWrapper = new ArrowWrapper (startPoint,endPoint);
//        	arrowWrapper.rotation = angle;
        }
      
		
		public function mouseDown(event:MouseEvent):void{
			if (mouseEventHelper.isArrowDraggingAllowed()){
				UraModelLocator.getInstance().mouseEventHelper.isMouseMovingArrow = true;
			}
		}
		
		public function rotate(event:DragEvent):void{
        	var isEndRotation:Boolean =  Boolean(event.dragSource.dataForFormat('rotateEnd'));
        	var isBeginingRotation:Boolean =  Boolean(event.dragSource.dataForFormat('rotateBegin'));
        	if (isEndRotation){
        		var startPoint:Point = arrowWrapper.startPoint;
        		arrowWrapper = new ArrowWrapper(startPoint,new Point(event.localX,event.localY));
        	}else if(isBeginingRotation){
        		var endPoint:Point = arrowWrapper.endPoint;
        		arrowWrapper = new ArrowWrapper(new Point(event.localX,event.localY),endPoint);
        	}
        	
        
        	
		}
		
		
		
		
		

	}
}